import axios from "axios";
import {userInfoStore} from "@/stores/counter.js";
import router from "@/router/index.js";
let config = {
    baseURL:'http://127.0.0.1:8000',
    timeout: 600*1000
}
const _axios = axios.create(config)
_axios.interceptors.request.use((config)=>{
    const info = userInfoStore()
    if (info.userToken){
        config.headers.Authorization = info.userToken;
    }
    return config
})
_axios.interceptors.response.use(function(response){
    const info = userInfoStore()
    if (response.data.code === 2002) {
        // 认证失败
        info.doLogout()
        router.replace({name: "login"});
    } else if (response.data.code === 2003) {
        //无权访问
        router.replace({name: "home"});
    }
    return response
},function(error){
    return Promise.reject(error)
})
export default _axios